package com.zerox.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import com.zerox.entity.SysUser;
import com.zerox.service.SysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    private LineCaptcha lineCaptcha  = null;


    @GetMapping("tologin")
    public String login() {
        return "login";
    }


    @PostMapping ("/login")
    @ResponseBody
    public String login2(HttpSession session,String username, String code, String password, String check, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        SysUser login = sysUserService.login(username, password);
        if(login!=null){
            if (code.equals(lineCaptcha.getCode())) {
                if(check!=null){
                    addCookie(username,password,response,request);
                }
                session.setAttribute("usersession","admin");
                return "1";
            } else {
                return "0";
            }
        }
        return "0";
    }

   @GetMapping("/getCode")
    public void getCode(HttpServletResponse response) throws IOException {
       // 随机生成 4 位验证码
       RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
       // 定义图片的显示大小
       lineCaptcha = CaptchaUtil.createLineCaptcha(115, 48);
       response.setContentType("image/jpeg");
       response.setHeader("Pragma", "No-cache");
       try {
           // 调用父类的 setGenerator() 方法，设置验证码的类型
           lineCaptcha.setGenerator(randomGenerator);
           // 输出到页面
           lineCaptcha.write(response.getOutputStream());
           // 打印日志
           System.out.println("生成的验证码:{}"+lineCaptcha.getCode());
           // 关闭流
           response.getOutputStream().close();
       } catch (IOException e) {
           e.printStackTrace();
       }
    }

    public static void addCookie(String userName,String password,HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        //创建cookie
        Cookie nameCookie = new Cookie("username",userName);
        Cookie passwordCookie = new Cookie("password",password);
        nameCookie.setPath(request.getContextPath()+"/");//设置cookie路径
        //设置cookie保存的时间 单位：秒
        nameCookie.setMaxAge(7*24*60*60);
        //将cookie添加到响应
        response.addCookie(nameCookie);
        response.addCookie(passwordCookie);
    }
}
